<?php
	session_start();
	include("checkTemplate.php");
	include("write_functions.php");
	connect("localhost", "root", "", "gradegriddatabase");
	include("fetchData.php");
	
	if($clean){
		// Insert to template table
		$data=fetchAccountDetails();
		createtemplate($_POST['template'],$data['account_id']);

		// Insert all majReq into major
		$data = fetchMostRecentTemplateId($_POST['template']);
		for($i=0;;$i++){
			if($_POST['majName'.$i]==NULL)
				break;
			if($_POST['majName'.$i]!="~NULL")	
				createmajor($data['template_id'],$_POST['majName'.$i],$_POST['majPer'.$i]);

			// Insert all minReq associated with current majReq
			$data2 = fetchMostRecentMajorId($data['template_id']);
			for($j=0;;$j++){
				if($_POST['minName'.$i.$j]==NULL)
					break;
				if($_POST['minName'.$i.$j]!="~NULL")
					createminor($data2['major_id'],$_POST['minName'.$i.$j],$_POST['minSco'.$i.$j]);
			}
		}
		mysql_close();
?>
		<head>
			<title>GradeGrid - Template Creation Success</title>
		</head>
		<body>
			<div style="position:absolute; top:-60px; margin-top:50%; left:-183px; margin-left:50%;">
				<h3 align="center">Your template had been successfully created!<br/>What would you like to do?</h3>
				<div align="center">
					<a href="addTemplate.php">Add a new template</a>
					 OR 
					<a id="close" onClick="window.close()" style="text-decoration:underline; color:#03F" onMouseOver="document.getElementById('close').style.cursor='pointer'">Close this window</a>
				</div>
			</div>
			<div style="font-size:9px; position:absolute; bottom:10px; left:-40px; margin-left:50%;" align="center">GradeGrid Copyright</div>
		</body>
<?php } else { ?>
		<!-- Populate hidden form then pass to addTemplate -->
        <form action="addTemplate.php?retry=1" id="dummy" style="display:none" method="post">
        <?php
			// Insert to template name to dummy
			echo "<input type=\"text\" name=\"template\" value=\"".$_POST['template']."\">";
				
			// Insert all majReq into dummy
			$k=0;
			for($i=0;;$i++){		
				if($_POST['majName'.$i]==NULL)
					break;
				if($_POST['majName'.$i]!="~NULL"){	
					echo "<input type=\"text\" name=\"majName".$k."\" value=\"".$_POST['majName'.$i]."\">";
					echo "<input type=\"text\" name=\"majPer".$k."\" value=\"".$_POST['majPer'.$i]."\">";
					$l=0;
					// Insert all minReq associated with current majReq
					for($j=0;;$j++){
						if($_POST['minName'.$i.$j]==NULL)
							break;
						if($_POST['minName'.$i.$j]!="~NULL"){
							echo "<input type=\"text\" name=\"minName".$k.$l."\" value=\"".$_POST['minName'.$i.$j]."\">";
							echo "<input type=\"text\" name=\"minSco".$k.$l."\" value=\"".$_POST['minSco'.$i.$j]."\">";
							$l++;
						}
					}
					$k++;
				}
			}
		?>
        </form>
<?php
        echo $error;
		echo "<br/><a id=\"close\" onClick=\"document.getElementById('dummy').submit()\" style=\"text-decoration:underline; color:#03F\" onMouseOver=\"document.getElementById('close').style.cursor='pointer'\">Click here to return to your form</a></div>";
	}
?>